package com.ld.caodan.entity;

import java.io.Serializable;

import org.jsoup.nodes.Element;

import com.ld.caodan.util.NumberUtils;
import com.ld.caodan.util.StringUtils;

/**
 * 
 * 
 * @author 刘笛 2011-10-31 上午11:54:08
 */
public class Article implements Serializable {
	/**
     * 
     */
	private static final long serialVersionUID = 1L;
	/**
	 * id
	 */
	private String id;
	/**
	 * 作者
	 */
	private String author;
	/**
	 * 日期
	 */
	private String date;
	/**
	 * 栏目
	 */
	private String column;
	/**
	 * 内容
	 */
	private String content;
	/**
	 * 内容详细信息（）
	 */
	private String contentLink;
	
	private int ding;
	private int cai;

	public Article(String str, String content) {
		this.id = NumberUtils.getNum(str.substring(0, str.indexOf("#")));
		int s = str.indexOf("by：");
		this.date = str.substring(str.indexOf("-") + 1, s);
		this.author = str.substring(s + 3, str.lastIndexOf("-"));
		this.column = str.substring(str.lastIndexOf("-") + 1);
		//TODO 这个地方每次都获取内容的话超级浪费流量的   所以以后要改为 先判断是否符合正则  再进行HttpClient获取
		this.content = content;
	}

	public Article(Element element) {
		this(element.child(0).text(), element.child(1).text());
		if(element.child(1)!=null&&element.child(1).childNodes().size()>0){
			contentLink = CaoDanUrl.HOMEPAGE + element.child(1).child(0).attr("href");
		}
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getColumn() {
		return column;
	}

	public void setColumn(String column) {
		this.column = column;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
	
	public String getContentLink() {
		return contentLink;
	}

	public void setContentLink(String contentLink) {
		this.contentLink = contentLink;
	}

	@Override
	public String toString() {
		return
		// "id:"+author+"\n\r"+
		// "时间:"+date+"\n\r"+
		// "作者:"+author+"\n\r"+
		// "栏目:"+column+"\n\r"+
		"内容:" + cutContent(content);
	}

	int lineLength = 50;

	String cutContent(String content) {
		if (content.length() <= lineLength)
			return content;
		int i = 0;
		StringBuffer buffer = new StringBuffer();
		while (i * lineLength < content.length()) {
			String tempContent = "";
			if (StringUtils
					.isNotBlank((tempContent = content
							.substring(i * lineLength,
									(i * lineLength + lineLength > content
											.length() ? content.length() : i
											* lineLength + lineLength))))) {
				buffer.append(tempContent + "\n");
				i++;
			}
		}
		return buffer.toString();
	}

	public int getDing() {
		return ding;
	}

	public void setDing(int ding) {
		this.ding = ding;
	}

	public int getCai() {
		return cai;
	}

	public void setCai(int cai) {
		this.cai = cai;
	}
	
	
}
